package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Setmeal;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R pages(Integer page,Integer pageSize,String name){
        Page<SetmealDto> dtoPage = setmealService.findByPage(page, pageSize, name);
        return R.success(dtoPage);
    }

    @DeleteMapping
    public R deletedByIds(Long[]ids){
        setmealService.deleteIds(ids);
        return R.success("批量删除成功");
    }

    @PostMapping("/status/{status}")
    public R updateStutus(@PathVariable int status,Long[]ids){
        setmealService.updateStatus(status, ids);
       return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        SetmealDto setmealDto=setmealService.findById(id);

        return R.success(setmealDto);
    }

    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){

        setmealService.updateAll(setmealDto);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R list(Setmeal setmeal){
        List<Setmeal> list = setmealService.listByid(setmeal);
        return R.success(list);
    }
}    